package com.qdxht.dispenser.base.action;

import android.app.Activity;
import android.app.Application;

import java.util.Stack;

/**
 * a tool to manage activity.
 */
public class ActivityManager1 {
	private static Stack<Activity> activityStack;
	private static ActivityManager1 instance;

	/** 当前应用上下文对象 */
	private Application mApplication;

	private ActivityManager1() {
	}

	public static ActivityManager1 getInstance() {
		if(instance == null) {
			synchronized (ActivityManager1.class) {
				if(instance == null) {
					instance = new ActivityManager1();
				}
			}
		}
		return instance;
	}

	public void init(Application application) {
		mApplication = application;
	}

	/**
	 * finish the activity and remove it from stack.
	 * @param activity
	 */
	public void popActivity(Activity activity) {
		if(activityStack==null)return;
		if (activity != null) {
			activity.finish();
			activity.overridePendingTransition(0, 0);
			activityStack.remove(activity);
		}
	}

	/**
	 * get the current activity.
	 * @return
	 */
	public Activity currentActivity() {
		if(activityStack==null || activityStack.isEmpty()){
			return null;
		}
		Activity activity = activityStack.lastElement();
		return activity;
	}

	/**
	 * get the first activity in the stack.
	 * @return
	 */
	public Activity firstActivity() {
		if(activityStack==null || activityStack.isEmpty()){
			return null;
		}
		Activity activity =  activityStack.firstElement();
		return activity;
	}


	/**
	 * add the activity to the stack.
	 * @param activity
	 */
	public void pushActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}

	/**
	 * remove all activity.
	 */
	public void popAllActivity() {
		if(activityStack==null){
			return;
		}
		while (true) {
			if (activityStack.empty()) {
				break;
			}
			Activity activity = currentActivity();
			popActivity(activity);
		}
	}

	/**
	 * remove all activity but keep the current activity.
	 */
	public void popAllActivityWithOutCurrent() {
		Activity activity = currentActivity();
		while (true) {
			if (activityStack.size() == 1) {
				break;
			}
			if (firstActivity() == activity) {
				break;
			} else {
				popActivity(firstActivity());
			}
		}
	}


	/**
	 * 销毁所有的 Activity
	 *
	 * @param classArray            白名单 Activity
	 */
	@SafeVarargs
	public final void finishAllActivities(Class<? extends Activity>... classArray) {
		while (true) {
			Activity activity = currentActivity();
			if (activityStack.size() == 1) {
				break;
			}
			boolean whiteClazz = false;
			if (classArray != null) {
				for (Class<? extends Activity> clazz : classArray) {
					if (activity.getClass().equals(clazz)) {
						whiteClazz = true;

					}
				}
			}
			if (whiteClazz) {
				continue;
			} else {
				popActivity(firstActivity());
			}
		}
	}

	/**
	 * get current activity count
	 * @return
     */
	public int getActivityCount(){
        if(activityStack!=null){
            return activityStack.size();
        }
        return 0;
    }

    public boolean isActivityInStack(Activity activity){
		if(activityStack==null||activityStack.size()==0){
			return false;
		}
		return activityStack.contains(activity);
	}

	public Stack<Activity> getStack(){
		return activityStack;
	}

	public Application getApplication() {
		return mApplication;
	}

}
